package com.jy.rhin.support.interceptor.convertor;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.jy.rhin.application.enums.AuditSourceTypeCodeEnum;
import com.jy.rhin.application.enums.CodeSystemEnum;
import com.jy.rhin.application.enums.EventActionCodeEnum;
import com.jy.rhin.application.enums.EventTypeCodeEnum;
import com.jy.rhin.application.enums.ParticipantObjectDataLifeCycleEnum;
import com.jy.rhin.application.enums.ParticipantObjectIDTypeCodeEnum;
import com.jy.rhin.application.enums.ParticipantObjectTypeCodeEnum;
import com.jy.rhin.application.enums.ParticipantObjectTypeCodeRoleEnum;
import com.jy.rhin.application.enums.ServiceCodeEnum;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;

import cn.org.chiss.rhin._2015.registration.person.PersonRecordQuery;
import cn.org.chiss.rhin._2015.registration.person.PersonRecordQueryResponse;

@Service("AuditInfoConverter4TerminologyPersonRecordQuery")
public class AuditInfoConverter4TerminologyPersonRecordQuery extends BaseAuditInfoConverter<PersonRecordQuery, PersonRecordQueryResponse> {

    public AuditInfoConverter4TerminologyPersonRecordQuery() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.PERSON_REGISTRATION,
                EventTypeCodeEnum.PERSON_DEMOGRAPHICS_QUERY,
                true,
                ParticipantObjectIDTypeCodeEnum.URI,
                ParticipantObjectTypeCodeRoleEnum.RESOURCE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("terminology-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.PERSON_REGISTRATION.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(PersonRecordQuery request, PersonRecordQueryResponse response) {
        return new AuditParticipantUserDesc("client-1", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(PersonRecordQuery request, PersonRecordQueryResponse response) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(PersonRecordQuery request, PersonRecordQueryResponse response) {
        return new AuditParticipantUserDesc("terminology-service", ServiceCodeEnum.PERSON_REGISTRATION.getName());
    }

    @Override
    String getObjectId(PersonRecordQuery request, PersonRecordQueryResponse response) {
        return "";
    }

	@Override
	public boolean isMatch(String method, Object data) {
		return "PersonInfoProviderPortImpl.personRecordQuery".equals(method);
	}

}
